package com.aaa.controller;

import com.aaa.service.T_QuestionBankService;
import com.aaa.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: TT
 * @Date: 2019/3/18 14:31
 * @Version 1.0
 */
@Controller
@RequestMapping("/questionbank/")
public class T_QuestionBankController {
@Autowired
    private T_QuestionBankService service;
    private ResourceLoader resourceLoader;//上传文件辅助类
    @Value("${upload.path}")
    private String filePath;
    public T_QuestionBankController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
@RequestMapping("go")
    public String  go(){
        return  "questionbank/questionbank";
    }
    @RequestMapping("list")
    @ResponseBody
    public  Map list(@RequestBody Map map){
        Map map1=new HashMap();
        List<Map>   data=service.getList(map);
        Integer  total=service.getCount(map);
         map1.put("data",data);
         map1.put("total",total);
         return  map1;
}
        /*
        *   上传文件
        * */
    @RequestMapping("upload")
    @ResponseBody
    public  Object upload(@RequestParam("file") MultipartFile file){
        String upFile=null;
        if (file!=null&&!file.isEmpty()){
            //获取文件路径
            upFile = FileUtil.uploadFile(filePath,file);
            //获取上传文件的名字
            String filename=file.getOriginalFilename();
    }
        return upFile;
    }
        /*
        *   下载文件
        * */
    @RequestMapping("/downLoad")
        public void downFile( String fileName,HttpServletResponse response){
            FileUtil.downloadFile(filePath,fileName,response);
    }
    /*
    *   删除文件
    * */
    @RequestMapping("del/{id}")
    @ResponseBody
    public  Integer del(@PathVariable Integer id){
    return  service.delete(id);
    }
    /**
     * 添加学习资料
     */
    @RequestMapping("/add")
    @ResponseBody
    public int add(@RequestBody Map map){
        return  service.add(map);
    }

}
